package cn.com.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.com.mall.product.entity.AttrAttrgroupRelationEntity;
import cn.com.mall.product.service.AttrAttrgroupRelationService;
import cn.com.mall.product.vo.AttrResVo;
import cn.com.mall.product.vo.AttrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import cn.com.mall.product.entity.AttrEntity;
import cn.com.mall.product.service.AttrService;
import cn.com.mall.common.utils.PageUtils;
import cn.com.mall.common.utils.R;

import javax.websocket.server.PathParam;


/**
 * 商品属性
 *
 * @author yangwen
 * @email yangwen@servyou.com.cn
 * @date 2020-04-09 23:52:34
 */
@RestController
@RequestMapping("product/attr")
@RefreshScope
public class AttrController {
    @Value("${username}")
    public String username;

    @Value("${password}")
    public String password;

    @RequestMapping("/test")
    public String test() {
        return username + "-" + password;

    }



    @Autowired
    private AttrService attrService;


    @GetMapping("/{attrTypeStr}/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId, @PathVariable("attrTypeStr") String attrTypeStr) {

        Long attrType = "base".equalsIgnoreCase(attrTypeStr) ? 1l : 0l;
        PageUtils page = attrService.queryPage(params, catelogId, attrType);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {


        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")

    public R info(@PathVariable("attrId") Long attrId) {

//		AttrEntity attr = attrService.getById(attrId);

        AttrResVo attrResVo = attrService.getAttrInfoById(attrId);

        return R.ok().put("attr", attrResVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVO attr) {
        attrService.save(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVO attr) {
//		attrService.updateById(attr);

        attrService.updateAttr(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
